﻿using MySql.Data.MySqlClient;
using PelitaProduksi.Controller;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PelitaProduksi.Model
{
    class Rajut : BaseProduksi
    {
        public Rajut()
        {
            list = new List<RajutDetail>();
        }
        string noReg;

        public string NoReg
        {
            get { return noReg; }
            set { noReg = value; }
        }
        List<RajutDetail> list;

        internal List<RajutDetail> List
        {
            get { return list; }
        }
        int shift;  // shiftx

        public int Shift
        {
            get { return shift; }
            set { shift = value; }
        }
        string tanggal;

        public string Tanggal
        {
            get { return tanggal; }
            set { tanggal = value; }
        }
        string nama1, nama2, nama3;

        public string Nama3
        {
            get { return nama3; }
            set { nama3 = value; }
        }

        public string Nama2
        {
            get { return nama2; }
            set { nama2 = value; }
        }

        public string Nama1
        {
            get { return nama1; }
            set { nama1 = value; }
        }

        Pegawai pegawai1, pegawai2, pegawai3;

        public Pegawai Pegawai3
        {
            get { return pegawai3; }
            set { pegawai3 = value; }
        }

        public Pegawai Pegawai2
        {
            get { return pegawai2; }
            set { pegawai2 = value; }
        }

        public Pegawai Pegawai1
        {
            get { return pegawai1; }
            set { pegawai1 = value; }
        }

        //double gaji1, gaji2, gaji3;
        //double gtek1, gtek2, gtek3;
        bool hadir1, hadir2, hadir3;

        public bool Hadir3
        {
            get { return hadir3; }
            set { hadir3 = value; }
        }

        public bool Hadir2
        {
            get { return hadir2; }
            set { hadir2 = value; }
        }

        public bool Hadir1
        {
            get { return hadir1; }
            set { hadir1 = value; }
        }
        string keterangan;

        public string Keterangan
        {
            get { return keterangan; }
            set { keterangan = value; }
        }
        string updateTime;

        public string UpdateTime
        {
            get { return updateTime; }
            set { updateTime = value; }
        }
        string updateDate;

        public string UpdateDate
        {
            get { return updateDate; }
            set { updateDate = value; }
        }
        string user;

        public string User
        {
            get { return user; }
            set { user = value; }
        }
        const string idkop = "RJ";
        int total;  // totprod

        public int Total
        {
            get { return total; }
            set { total = value; }
        }

        public static Rajut getRajut(string id)
        {
            if (id == null) return null;
            Rajut r = new Rajut();
            string query = string.Format(
                "select r.`No`, r.keterangan, k1.nama, k2.nama, k3.nama, r.hadir1, r.hadir2, r.hadir3, r.tupdate, r.jupdate, r.usrupdate, r.shiftx, r.totprod, r.tanggal, r.nama1, r.nama2, r.nama3 " +
                "from rajut r " +
                " inner join karyawan k1 on k1.kode=r.nama1 " +
                " inner join karyawan k2 on k2.kode=r.nama2 " +
                " inner join karyawan k3 on k3.kode=r.nama3 " +
                "where r.`No` = '{0}' " +
                "order by r.tanggal asc",
                id
                );
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader(query);
            while (reader.Read())
            {
                r.NoReg = reader.GetString(0);
                r.Keterangan = reader.GetString(1);
                r.Nama1 = reader.GetString(2);
                r.Nama2 = reader.GetString(3);
                r.Nama3 = reader.GetString(4);
                r.Hadir1 = reader.GetBoolean(5);
                r.Hadir2 = reader.GetBoolean(6);
                r.Hadir3 = reader.GetBoolean(7);
                r.UpdateDate = reader.GetDateTime(8).ToShortDateString();
                r.UpdateTime = reader.GetString(9);
                r.User = reader.GetString(10);
                r.Shift = reader.GetInt32(11);
                r.Total = reader.GetInt32(12);
                r.Tanggal = reader.GetString(13);
                r.Pegawai1 = new Pegawai(reader.GetString(14), reader.GetString(2));
                r.Pegawai2 = new Pegawai(reader.GetString(15), reader.GetString(3));
                r.Pegawai3 = new Pegawai(reader.GetString(16), reader.GetString(4));
            }
            db.Close();
            return r;
        }

        public static List<Rajut> getRajut(string tanggalAwal, string tanggalAkhir)
        {
            if (tanggalAwal == null || tanggalAkhir == null) return null;
            List<Rajut> list = new List<Rajut>();
            string query = string.Format(
                "select r.`No`, r.keterangan, k1.nama, k2.nama, k3.nama, r.hadir1, r.hadir2, r.hadir3, r.tupdate, r.jupdate, r.usrupdate, r.shiftx, r.totprod, r.tanggal "+
                "from rajut r " +
                " inner join karyawan k1 on k1.kode=r.nama1 "+
                " inner join karyawan k2 on k2.kode=r.nama2 " +
                " inner join karyawan k3 on k3.kode=r.nama3 " +
                "where r.tanggal between '{0}' and '{1}' "+
                "order by r.tanggal asc",
                tanggalAwal, tanggalAkhir
                );
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader(query);
            while(reader.Read())
            {
                Rajut r = new Rajut();
                r.NoReg = reader.GetString(0);
                r.Keterangan = reader.GetString(1);
                r.Nama1 = reader.GetString(2);
                r.Nama2 = reader.GetString(3);
                r.Nama3 = reader.GetString(4);
                r.Hadir1 = reader.GetBoolean(5);
                r.Hadir2 = reader.GetBoolean(6);
                r.Hadir3 = reader.GetBoolean(7);
                r.UpdateDate = reader.GetDateTime(8).ToShortDateString();
                r.UpdateTime = reader.GetString(9);
                r.User = reader.GetString(10);
                r.Shift = reader.GetInt32(11);
                r.Total = reader.GetInt32(12);
                r.Tanggal = reader.GetDateTime(13).ToShortDateString();
                list.Add(r);
            }
            db.Close();
            return list;
        }
        
        public override void insert()
        {
            int hadir1 = Hadir1 ? 1 : 0;
            int hadir2 = Hadir2 ? 1 : 0;
            int hadir3 = Hadir3 ? 1 : 0;
            MySQLController db = new MySQLController();
            db.Open();
            MySqlTransaction tr = db.BeginTransaction();
            string queryInsertRajut = string.Format(
                "insert into {0} (No, tanggal, keterangan, nama1, nama2, nama3, hadir1, hadir2, hadir3, jmlHadir, idKop, tupdate, jupdate, usrupdate, shiftx, totprod) "+
            "VALUES('{1}', '{2}', '{3}', '{4}', '{5}', '{6}', {7}, {8}, {9}, {10}, '{11}', '{12}', '{13}', '{14}', {15}, {16})",
                "rajut",
                this.NoReg,
                this.Tanggal,
                this.Keterangan,
                this.Nama1,
                this.Nama2,
                this.Nama3,
                hadir1,
                hadir2,
                hadir3,
                hadir1+hadir2+hadir3,
                idkop,
                this.updateDate,
                this.UpdateTime,
                this.User,
                this.Shift,
                this.Total
                );
            try
            {
                db.ExecuteNonQuery(queryInsertRajut);
                int id = 1;
                foreach (RajutDetail rd in list)
                {
                    string q = string.Format("insert into {0}"+
                        "(No, id, barang, nolap, rajut) "+
                        "VALUES('{1}', {2}, '{3}', '{4}', {5}) ",
                        "rajut_det",
                        this.NoReg,
                        id,
                        rd.KodeBarang,
                        rd.Nolap,
                        rd.Rajut
                        );
                    db.ExecuteNonQuery(q);
                    id++;
                }
                tr.Commit();
            }
            catch (Exception)
            {
                tr.Rollback();
            }
            db.Close();
        }

        public override void edit(string id)
        {

        }
    }
}
